package example;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 状态机状态多例模式
 */
public class StateMultiton {
    private static final Map<String, StateMultiton> instances = new ConcurrentHashMap<>();
    
    private final String stateName;
    private final String description;
    
    private StateMultiton(String stateName, String description) {
        this.stateName = stateName;
        this.description = description;
    }
    
    static {
        // 预定义所有状态实例
        instances.put("INIT", new StateMultiton("INIT", "初始状态"));
        instances.put("PROCESSING", new StateMultiton("PROCESSING", "处理中状态"));
        instances.put("SUCCESS", new StateMultiton("SUCCESS", "成功状态"));
        instances.put("FAILED", new StateMultiton("FAILED", "失败状态"));
        instances.put("CANCELLED", new StateMultiton("CANCELLED", "取消状态"));
    }
    
    public static StateMultiton getInstance(String stateName) {
        StateMultiton instance = instances.get(stateName);
        if (instance == null) {
            throw new IllegalArgumentException("未知状态: " + stateName);
        }
        return instance;
    }
    
    public static Map<String, StateMultiton> getAllStates() {
        return new ConcurrentHashMap<>(instances);
    }
    
    public String getStateName() {
        return stateName;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void handle() {
        System.out.println("处理状态: " + stateName + " - " + description);
    }
    
    @Override
    public String toString() {
        return "StateMultiton{stateName='" + stateName + "', description='" + description + "'}";
    }
}